﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Devices.Sensors;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace Program_Sensor
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class MainPage : Program_Sensor.Common.LayoutAwarePage
    {
        SimpleOrientationSensor orientationSensor = Windows.Devices.Sensors.SimpleOrientationSensor.GetDefault();
        Accelerometer accelerometer = Windows.Devices.Sensors.Accelerometer.GetDefault();
        Gyrometer gyrometer = Windows.Devices.Sensors.Gyrometer.GetDefault();
        Compass compass = Windows.Devices.Sensors.Compass.GetDefault();
        LightSensor lightSensor = Windows.Devices.Sensors.LightSensor.GetDefault();

        


        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

        private void Accelerometer_Click(object sender, RoutedEventArgs e)
        {
            if (accelerometer != null)
            {
                accelerometer.ReadingChanged += accelerometer_ReadingChanged;
            }
        }

        async void accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                accel_outputX.Text = args.Reading.AccelerationX.ToString();
                accel_outputY.Text = args.Reading.AccelerationY.ToString();
                accel_outputZ.Text = args.Reading.AccelerationZ.ToString();
            });
        }

        private void Orientation_Click(object sender, RoutedEventArgs e)
        {
            if (orientationSensor != null)
            {
                orientationSensor.OrientationChanged += orientationSensor_OrientationChanged;
            }
        }

        async void orientationSensor_OrientationChanged(SimpleOrientationSensor sender, SimpleOrientationSensorOrientationChangedEventArgs args)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                orientation_output.Text = args.Orientation.ToString();
            });
        }

        private void Gyro_Click(object sender, RoutedEventArgs e)
        {
            if (gyrometer !=null)
                gyrometer.ReadingChanged += gyrometer_ReadingChanged;
        }

        async void gyrometer_ReadingChanged(Gyrometer sender, GyrometerReadingChangedEventArgs args)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                vel_accel_outputX.Text = args.Reading.AngularVelocityX.ToString();
                vel_accel_outputY.Text = args.Reading.AngularVelocityY.ToString();
                vel_accel_outputZ.Text = args.Reading.AngularVelocityZ.ToString();
            });
        }

        private void Compas_Click(object sender, RoutedEventArgs e)
        {
            if (compass !=null)
                compass.ReadingChanged += compass_ReadingChanged;
        }

        async void compass_ReadingChanged(Compass sender, CompassReadingChangedEventArgs args)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                compass_output.Text = args.Reading.HeadingTrueNorth.ToString();
            });
        }

        private void Light_Sensor_Click(object sender, RoutedEventArgs e)
        {
            if (lightSensor!=null)
                lightSensor.ReadingChanged += lightSensor_ReadingChanged;
        }

        async void lightSensor_ReadingChanged(LightSensor sender, LightSensorReadingChangedEventArgs args)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                light_output.Text = args.Reading.IlluminanceInLux.ToString();
            });
        }
    }
}
